package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.TaxRateDAO;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRate;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRate.Column;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRateExample;
import com.ecoolex.chinadutyfree.retailer.entity.TaxRateExample.Criteria;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryTaxRateDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TaxRateManager {
	
    @Autowired
    private TaxRateDAO dao;
    
    public TaxRate getTaxRate(Integer taxRateId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(taxRateId, TaxRate.NOT_DELETED);
	}

	public Pagination<TaxRate> queryTaxRate(QueryTaxRateDTO query, Sorting[] sorts, Page page) {
		TaxRateExample example = new TaxRateExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNull(query.getType())) {
			criteria.andTypeEqualTo(query.getType());
		}
		if(Check.notNull(query.getName())) {
			criteria.andNameLike("%"+query.getName()+"%");
		}
		if(Check.notNull(query.getTaxNo())) {
			criteria.andTaxNoEqualTo(query.getTaxNo());
		}
		if(Check.notNull(query.getLevel())) {
			criteria.andLevelEqualTo(query.getLevel());
		}
		if(Check.notNull(query.getLastTaxRateId())) {
			criteria.andLastTaxRateIdEqualTo(query.getLastTaxRateId());
		}
		criteria.andDeletedEqualTo(TaxRate.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public List<TaxRate> listByLastTaxRateId(Integer lastTaxRateId) {
		TaxRateExample example = new TaxRateExample();
		Criteria criteria = example.createCriteria();
		criteria.andLastTaxRateIdEqualTo(lastTaxRateId).andDeletedEqualTo(TaxRate.NOT_DELETED);
		return dao.selectByExample(example);
	}
	
	private void verify(TaxRate record) {
		BizAssert.isTrue(Check.notNull(record.getLastTaxRateId()),ResultCode.VERIFY.build(10), "上级税率Id为空");
		BizAssert.isTrue(Check.notNull(record.getType()),ResultCode.VERIFY.build(11), "税率种类为空");
		BizAssert.isTrue(Check.notNullOrEmpty(record.getName()), ResultCode.VERIFY.build(12), "名称为空");
		
	}

	public TaxRate addTaxRate(TaxRate record, String createUser) throws BizException {
		verify(record);
		//一级税率分类
		if(Check.eq(0, record.getLastTaxRateId())) {
			record.setLevel(1);
		}else {
			TaxRate last = getTaxRate(record.getLastTaxRateId());
			record.setLevel(last.getLevel()+1);
		}
		record.setDeleted(TaxRate.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public TaxRate editTaxRate(TaxRate record, String updateUser) throws BizException {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

	public void deleteTaxRate(TaxRate record, String updateUser) throws BizException {
		record.setDeleted(TaxRate.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record,Column.deleted,Column.updateUser,Column.updateTime);
	}
}